package com.rongshu.widget;


import android.app.Dialog;
import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.rongshu.R;

/**
 * created by zhengeweis on 2018/11/2.
 * description：app统一使用的dialog
 */

/*
 BanyanDialog.Builder builder = new BanyanDialog.Builder(getActivity());
 builder.setTitle("是否保存草稿？");
 builder.setContent("若不保存将彻底删除");
 builder.setLeft("删除");
 builder.setRight("保存");
 builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
        @Override
        public void leftClickListener() {
    }

        @Override
        public void rightClickListener() {
    }
});
 BanyanDialog dialog = builder.create();
 dialog.show();
 */

public class BanyanDialog extends Dialog {
    public BanyanDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        TextView titleTv;
        TextView contentTv;
        TextView leftTv;
        TextView rightTv;
        View line;
        private Context context; // 上下文对象
        private onClickListener listener;
        private int mMainLayout = R.layout.dialog_base;
        private String title;
        private String content;
        private String left;
        private String right;
        private boolean mCancelable = true;

        @ColorInt
        private int left_color;
        @ColorInt
        private int right_color;

        public interface onClickListener {
            void leftClickListener();

            void rightClickListener();
        }

        public Builder(Context context) {
            this.context = context;
            left_color = ContextCompat.getColor(context, R.color.color_3cc051);
            right_color = ContextCompat.getColor(context, R.color.color_3cc051);
        }

        public Builder setDialogOnClick(onClickListener listener) {
            this.listener = listener;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setLeft(String left) {
            this.left = left;
            return this;
        }

        public Builder setRight(String right) {
            this.right = right;
            return this;
        }

        public Builder setLeftColor(@ColorInt int color) {
            this.left_color = color;
            return this;
        }

        public Builder setRightColor(@ColorInt int color) {
            this.right_color = color;
            return this;
        }

        public Builder setCancelable(boolean isCancle) {
            this.mCancelable = isCancle;
            return this;
        }

        public BanyanDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final BanyanDialog dialog = new BanyanDialog(context, R.style.CustomDialogStyle);
            View layout = inflater.inflate(mMainLayout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            titleTv = layout.findViewById(R.id.title);
            contentTv = layout.findViewById(R.id.content);
            leftTv = layout.findViewById(R.id.left);
            rightTv = layout.findViewById(R.id.right);
            line = layout.findViewById(R.id.line2);
            titleTv.setText(title);
            contentTv.setText(content);
            leftTv.setText(left);
            rightTv.setText(right);
            leftTv.setTextColor(left_color);
            rightTv.setTextColor(right_color);
            titleTv.setVisibility(TextUtils.isEmpty(title) ? View.GONE : View.VISIBLE);
            contentTv.setVisibility(TextUtils.isEmpty(content) ? View.GONE : View.VISIBLE);
            leftTv.setVisibility(TextUtils.isEmpty(left) ? View.GONE : View.VISIBLE);
            rightTv.setVisibility(TextUtils.isEmpty(right) ? View.GONE : View.VISIBLE);
            line.setVisibility(TextUtils.isEmpty(left) || TextUtils.isEmpty(right) ? View.GONE : View.VISIBLE);
            leftTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.leftClickListener();
                    }
                }
            });
            rightTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.rightClickListener();
                    }
                }
            });
            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(mCancelable);
            return dialog;
        }

    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams params = this.getWindow().getAttributes();
        params.width = (int) getContext().getResources().getDimension(R.dimen.dd_dimen_530px);
        this.getWindow().setAttributes(params);
    }
}
